package com.android.memory;

import java.util.Random;

import com.android.memory.R;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;

public class ImageAdapter extends BaseAdapter {
	private Random rndGen = new Random();
	private Context mContext;
	private int width;
	private int height;
	private static int dif;
	
	/* Constructor for ImageAdapter
	 *  x,y is size of cards taken from GameActivity
	 *  Shuffles the cards
	 */
	public ImageAdapter(Context c, int x, int y, int d){
		mContext = c;
		width = x;
		height = y;
		dif = d;
		shuffleCards(getDifList(dif));
	}

	//Shuffles a list of images
    public void shuffleCards(Integer[] img){
        for(int i = 0;i < 3*img.length;i++) {                   
        	
        	int n = rndGen.nextInt(img.length);
            int m = rndGen.nextInt(img.length);
                        
            Integer picked1 = img[n];
            Integer picked2 = img[m];
                        
            img[m] = picked1;
            img[n] = picked2;                       
        }
    }
    
    // Returns length of the list of cards
	public int getCount() {
		return getDifList(dif).length;
	}
	
	public Object getItem(int position) {
		return null;
	}

	public long getItemId(int position) {
		return 0;
	}

	
	// Creates the list of cards to the view
	public View getView(int position, View convertView, ViewGroup parent) {
			Card imageView;
			if(convertView == null)	{
				imageView = new Card(mContext);
				
				//Sets the layout of the cards
				imageView.setLayoutParams(new GridView.LayoutParams(width, height));
				imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
				imageView.setPadding(8,8,8,8);
			}
			else {
				imageView = (Card) convertView;
			}
			
			// Returns an imageView (card) 
			imageView.setImageId(getDifList(dif)[position]);
			return imageView;
	}
	
	//Chooses a list of cards depending on difficulty
	private Integer[] getDifList(int diff) {
		if(diff ==3) 
			return mThumbIds3;
		else if(diff == 2) 	
			return mThumbIds2;
		else				
			return mThumbIds1;
	}
	

	private Integer[] mThumbIds1 = {
			R.drawable.card1,R.drawable.card1,
			R.drawable.card2,R.drawable.card2,
			R.drawable.card3,R.drawable.card3,
			R.drawable.card4,R.drawable.card4,
			R.drawable.card5,R.drawable.card5,
			R.drawable.card6,R.drawable.card6,
			R.drawable.card7,R.drawable.card7,
			R.drawable.card8,R.drawable.card8
			
	};

	private Integer[] mThumbIds2 = {
			R.drawable.card1,R.drawable.card1,
			R.drawable.card2,R.drawable.card2,
			R.drawable.card3,R.drawable.card3,
			R.drawable.card4,R.drawable.card4,
			R.drawable.card5,R.drawable.card5,
			R.drawable.card6,R.drawable.card6,
			R.drawable.card7,R.drawable.card7,
			R.drawable.card8,R.drawable.card8,
			R.drawable.card9,R.drawable.card9,
			R.drawable.card10,R.drawable.card10,
			R.drawable.card11,R.drawable.card11,
			R.drawable.card12,R.drawable.card12,
			R.drawable.card13,R.drawable.card13,
			R.drawable.card14,R.drawable.card14,
			R.drawable.card15,R.drawable.card15,
			R.drawable.card16,R.drawable.card16,
			R.drawable.card17,R.drawable.card17,
			R.drawable.card18,R.drawable.card18
	};

	private Integer[] mThumbIds3 = {
			R.drawable.card1,R.drawable.card1,
			R.drawable.card2,R.drawable.card2,
			R.drawable.card3,R.drawable.card3,
			R.drawable.card4,R.drawable.card4,
			R.drawable.card5,R.drawable.card5,
			R.drawable.card6,R.drawable.card6,
			R.drawable.card7,R.drawable.card7,
			R.drawable.card8,R.drawable.card8,
			R.drawable.card9,R.drawable.card9,
			R.drawable.card10,R.drawable.card10,
			R.drawable.card11,R.drawable.card11,
			R.drawable.card12,R.drawable.card12,
			R.drawable.card13,R.drawable.card13,
			R.drawable.card14,R.drawable.card14,
			R.drawable.card15,R.drawable.card15,
			R.drawable.card16,R.drawable.card16,
			R.drawable.card17,R.drawable.card17,
			R.drawable.card18,R.drawable.card18,
			R.drawable.card19,R.drawable.card19,
			R.drawable.card20,R.drawable.card20,
			R.drawable.card21,R.drawable.card21,
			R.drawable.card22,R.drawable.card22,
			R.drawable.card23,R.drawable.card23,
			R.drawable.card24,R.drawable.card24
	};
	
}
